const service = require('../request').service;
const model = require("../../utils/modelUtil");

/**
 * 副本类
 */
class Copy {
    constructor(user) {
        this.user = user;
    }

    /**
     * 获取副本名称
     * @param id 副本ID
     */
    getCopyName(id) {
        const data = {
            1: '危虎山',
            2: '哈拉奥'
        };
        return data[id];
    }

    /**
     * 获取副本信息
     * @param id 1=危虎山 2=哈拉奥
     * @returns {Promise<unknown>}
     */
    getData(id) {
        return new Promise((resolve, reject) => {
            service({
                method: 'POST',
                url: this.user.game.reqUrl + '/getCopyData',
                data: model.userModel('getCopyData', {copyID: id}, this.user.game.isWeb, this.user.game.data),
            }).then(res => {
                const resData = res.data;
                resolve(resData.code===200?resData.MsgData:{});
            }).catch(err => {
                reject(err);
            })
        })
    }

    /**
     * 扫荡
     * @param id 1=危虎山 2=哈拉奥
     * @param state 1=普通 2=困难
     * @param count 扫荡次数
     * @returns {Promise<unknown>}
     */
    sweep(id, state, count) {
        return new Promise((resolve, reject) => {
            service({
                method: 'POST',
                url: this.user.game.reqUrl + '/sweepCopy',
                data: model.userModel('sweepCopy', {copyID: id, state: state, sweepCount: count}, this.user.game.isWeb, this.user.game.data),
            }).then(res => {
                const resData = res.data;
                resolve(resData.code===200?resData.MsgData:{});
            }).catch(err => {
                reject(err);
            })
        })
    }

    /**
     * 进入副本
     * @param id 1=危虎山 2=哈拉奥
     * @param state 1=普通 2=困难
     * @returns {Promise<unknown>} bool
     */
    enter(id, state) {
        return new Promise((resolve, reject) => {
            service({
                method: 'POST',
                url: this.user.game.reqUrl + '/EneterCopy',
                data: model.userModel('EneterCopy', {copyID: id, state: state}, this.user.game.isWeb, this.user.game.data),
            }).then(res => {
                const resData = res.data;
                resolve(resData.code===200&&resData.MsgData.error===0&&resData.MsgData[resData.MsgData.p].eRet===1);
            }).catch(err => {
                reject(err);
            })
        })
    }

    /**
     * 离开副本
     * @param id 1=危虎山 2=哈拉奥
     * @returns {Promise<unknown>}
     */
    leave(id) {
        const data = {
            1: ["10017",2,"10018",1,"10019",1,"10020",1,"10021",1,"10030",2,"10031",2],
            2: ["10468",1,"10469",1,"10470",1,"10471",1,"10472",1,"10473",1,"10474",1,"10479",4,"10480",3,"10481",4,"10482",3]
        };
        return new Promise((resolve, reject) => {
            service({
                method: 'POST',
                url: this.user.game.reqUrl + '/LeaveCopy',
                data: model.userModel('LeaveCopy', {copyID: id, npcArr: data[id], flag: true}, this.user.game.isWeb, this.user.game.data),
            }).then(res => {
                const resData = res.data;
                resolve(resData.code===200?resData.MsgData:{});
            }).catch(err => {
                reject(err);
            })
        })
    }

    /**
     * 购买次数
     * @param id 1=危虎山 2=哈拉奥
     * @param count 次数
     * @returns {Promise<unknown>} bool
     */
    buyCount(id, count) {
        return new Promise((resolve, reject) => {
            service({
                method: 'POST',
                url: this.user.game.reqUrl + '/BuyChallengeCount',
                data: model.userModel('BuyChallengeCount', {copyID: id, buyCount: count}, this.user.game.isWeb, this.user.game.data),
            }).then(res => {
                const resData = res.data;
                resolve(resData.code===200&&resData.MsgData.error===0&&resData.MsgData[resData.MsgData.p].eRet===1);
            }).catch(err => {
                reject(err);
            })
        })
    }
}

module.exports = Copy;